package com.springboot.demo.controller;


import com.springboot.demo.model.MessageInfo;
import com.springboot.demo.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/message")
@RestController
public class message {
//    private List<message> msgsList=new ArrayList<>();
    @Autowired
    private MessageService messageService;

//    MessageService messageService=new MessageService();
    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        return messageService.selectAll();
    }

    @RequestMapping("/publish")
    public String publish(@RequestBody MessageInfo messageInfo){
        System.out.println("接收到参数，mes："+messageInfo);
        //1.校验参数
        //2.存储信息
        if(!StringUtils.hasLength(messageInfo.getMessage())
                || !StringUtils.hasLength(messageInfo.getTo())
                || !StringUtils.hasLength(messageInfo.getFrom())){
            System.out.println("参数不规范");
            return "false";
        }
        try{
            Integer result=messageService.insert(messageInfo);
            return result==1 ? "true" : "false";
        }catch(Exception e){
            log.error("发布流言失败，e",e);
            return "false";
        }

    }


}
